package com.blb.marrysystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.marrysystem.code.idUtils;
import com.blb.marrysystem.entity.*;
import com.blb.marrysystem.entity.dto.DetailsPackageDto;
import com.blb.marrysystem.entity.dto.DetailsRoomDto;
import com.blb.marrysystem.entity.dto.HotelDto;
import com.blb.marrysystem.entity.dto.HotelPackageDto;
import com.blb.marrysystem.service.TbHotelService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * (TbHotel)表控制层
 *
 * @author makejava
 * @since 2023-01-11 09:22:27
 */
@Controller
public class TbHotelController {
    private Integer begin=1;
    private String late="zonghe";
    /**
     * 服务对象
     */
    @Resource
    private TbHotelService tbHotelService;

    /**
     * 分页查询
     *

     */
    @RequestMapping("/hotelQueryByPage")
    public String queryByPage(@RequestParam(value = "page",required = false,defaultValue = "") String page, Model model,@RequestParam(value = "latest",required = false,defaultValue = "")String latest,@RequestParam(value = "pages",required = false,defaultValue = "")String pagess,@RequestParam(value = "search",required = false,defaultValue = "")String search) {
        if(!"".equals(page)){
            begin=Integer.parseInt(page);
        }
        if("".equals(latest)){
            late="zonghe";
        }
        if("综合".equals(latest)){
            late="zonghe";
        }
        if("最新".equals(latest)){
            late="zuixin";
        }
        if(begin<1){
            begin=1;
        }
        if(!"".equals(pagess)){
            if(begin>Integer.parseInt(pagess)){
                begin=Integer.parseInt(pagess);
            }
        }
        Page<HotelDto> hotelDtoPage = tbHotelService.queryByPage(begin,late,search.trim());
        List<HotelDto> records = hotelDtoPage.getRecords();
        long pages = hotelDtoPage.getPages();
        long total = hotelDtoPage.getTotal();
        if(begin>Integer.parseInt(String.valueOf(pages))){
            begin=1;
            hotelDtoPage = tbHotelService.queryByPage(begin,late,search.trim());

        }
        records = hotelDtoPage.getRecords();
        pages = hotelDtoPage.getPages();
        total = hotelDtoPage.getTotal();
        //获取销量排前的酒店
        List<HotelPackageDto> hotelPackageDtoList=tbHotelService.selectHotelList();
        model.addAttribute("hotelDtoPageList",records);
        model.addAttribute("pages",pages);
        model.addAttribute("total",total);
        model.addAttribute("begin",begin);
        model.addAttribute("search",search);
        model.addAttribute("hotelPackageDtoList",hotelPackageDtoList);
        return "field";
    }
    @RequestMapping("/wedding")
    public String weddings(){
        return "wedding";
    }
    @RequestMapping("/detailsList")
    public String details(HotelDto hotelDto,Model model){
        //婚宴场地列表
        List<DetailsRoomDto> detailsRoomDtoList= tbHotelService.detailsRoomList(hotelDto);
        //酒店信息
        List<TbInformation> tbInformation1=tbHotelService.detailsInformation(hotelDto);
        TbInformation tbInformation = tbInformation1.get(0);
        //套餐
        List<DetailsPackageDto> detailsPackageDtoList=tbHotelService.detailsPackage(hotelDto);
        //酒店简略信息
        model.addAttribute("hotelDto",hotelDto);
        //酒店婚宴场地列表
        model.addAttribute("detailsRoomDtoList",detailsRoomDtoList);
        //酒店信息
        model.addAttribute("tbInformation",tbInformation);
        //套餐列表
        model.addAttribute("detailsPackageDtoList",detailsPackageDtoList);
        return "details";
    }
    @RequestMapping("/packageDetailsList")
    public String packageDetailsList(HotelDto hotelDto,@RequestParam(value = "packageId",required = false,defaultValue = "") String packageId,Model model){
        //婚宴场地列表
        List<DetailsRoomDto> detailsRoomDtoList= tbHotelService.detailsRoomList(hotelDto);
        DetailsRoomDto detailsRoomDto = detailsRoomDtoList.get(0);
        List<DetailsRoomDto> detailsRoomDtoLists = new ArrayList<>();
        for(int o=1;o<detailsRoomDtoList.size();o++) {
            detailsRoomDtoLists.add(detailsRoomDtoList.get(o));
        }
        //酒店信息
        List<TbInformation> tbInformation1=tbHotelService.detailsInformation(hotelDto);
        TbInformation tbInformation = tbInformation1.get(0);
        //套餐信息
        TbPackage packageInformation =tbHotelService.packageInformation(packageId);
        //其他套餐信息
        List<DetailsPackageDto> detailsPackageDtoList=tbHotelService.otherPackageInformation(packageId,hotelDto.getTbHotelHotelId());
        //酒店列表
        List<HotelDto> hotelDtoList = tbHotelService.hotelList();
        //酒店简略信息
        model.addAttribute("hotelDto",hotelDto);
        //酒店婚宴场地列表
        model.addAttribute("detailsRoomDto",detailsRoomDto);
        model.addAttribute("detailsRoomDtoLists",detailsRoomDtoLists);
        //酒店信息
        model.addAttribute("tbInformation",tbInformation);
        //套餐信息
        model.addAttribute("packageInformation",packageInformation);
        //酒店列表
        List<HotelDto> hotelDtos = new ArrayList<>();
        int size=new Random().nextInt(hotelDtoList.size());
        while (true){
            int size1=new Random().nextInt(hotelDtoList.size());
            if(size1==size){
                continue;
            }else {
                size =size1;
            }
            hotelDtos.add(hotelDtoList.get(size));
            if(hotelDtos.size()==2){
                break;
            }
        }
        model.addAttribute("hotelDtoList",hotelDtos);
        //其他套餐信息列表
        model.addAttribute("detailsPackageDtoList",detailsPackageDtoList);
        //原价套餐信息
        return "packageDetails";
    }
    @ResponseBody
    @RequestMapping("/loginText")
    public Object loginText(HttpSession session){
        TbUser user = (TbUser)session.getAttribute("user");
        if(user==null){
            return "0";
        }else {
            return "1";
        }
    }
    @RequestMapping("/loginHtml")
    public String loginHtml(){
        return "login";
    }
    @RequestMapping("/login")
    public String login(String telephoneNumber,String password,String code,HttpSession session,Model model){
        if(!code.equals(session.getAttribute("code"))){
            model.addAttribute("telephoneNumber",telephoneNumber);
            model.addAttribute("password",password);
            model.addAttribute("codeerror","验证码错误");
            return "login";
        }
        try {
            TbUser user = tbHotelService.login(telephoneNumber,password);
            if(session.getAttribute("user")!=null){
                session.removeAttribute("user");
            }
            session.setAttribute("user",user);
            return "wedding";
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("账户不存在或密码错误");
            model.addAttribute("telephoneNumber",telephoneNumber);
            model.addAttribute("password",password);
            model.addAttribute("pwderror","账户不存在或密码错误");
            return "login";
        }
    }
    @RequestMapping("/registerHtml")
    public String registerHtml(){
        return "register";
    }
    @RequestMapping("/register")
    public String register(String telephoneNumber,String passWord,HttpSession session,Model model){
        try {
            tbHotelService.register(telephoneNumber,passWord);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("userexists","账户已存在");
            model.addAttribute("telephoneNumber",telephoneNumber);
            model.addAttribute("password",passWord);
            return "register";
        }
        return "login";
    }
    @RequestMapping("/exit")
    public String exit(HttpSession session){
        if(session.getAttribute("user")!=null){
            session.removeAttribute("user");
        }
        return "wedding";
    }
    @RequestMapping("/personalDataHtml")
    public String personalData(HttpSession session,Model model){
        if(session.getAttribute("user")!=null){
            TbUser user=(TbUser)session.getAttribute("user");
            model.addAttribute("user",user);
            return "personal_data";
        }else {
            return "login";
        }
    }
    @RequestMapping("/shopCartHtml")
    public String shopCart(HttpSession session,Model model){
        if(session.getAttribute("user")!=null){
            TbUser user=(TbUser)session.getAttribute("user");
            List<TbShoppingcart> shoppingcartList=tbHotelService.shopCart(user.getUserTel());
            model.addAttribute("user",user);
            model.addAttribute("shoppingcartList",shoppingcartList);
            return "Shopping_Cart";
        }else {
            return "login";
        }
    }
    @RequestMapping("/orderHtml")
    public String order(HttpSession session,Model model){
        if(session.getAttribute("user")!=null){
            TbUser user=(TbUser)session.getAttribute("user");
            List<TbOrder> orderList = tbHotelService.order(user.getUserTel());
            model.addAttribute("orderList",orderList);
            model.addAttribute("user",user);
            return "Order";
        }else {
            return "login";
        }
    }
    @ResponseBody
    @RequestMapping("/shopPingCart")
    public String shopPingCart(String shopPingCartName, String shopPingCartType, String shopPingCartPlace, String shopPingCartPrice, String shopPingCartCount, String shopPingCartImg, String shopPingCartHotelName, HttpSession session){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time = now.format(dateTimeFormatter);
        TbUser user = (TbUser)session.getAttribute("user");
        TbShoppingcart tbShoppingcart = new TbShoppingcart(idUtils.suijiutils(),time,shopPingCartName,shopPingCartType,shopPingCartPlace,shopPingCartPrice,shopPingCartCount,user.getUserTel(),shopPingCartImg,shopPingCartHotelName);
        tbHotelService.shopPingCartInsert(tbShoppingcart);
        return "1";
    }
    @RequestMapping("/computer")
    public String computer(String number , String shopId){
        tbHotelService.computer(number,shopId);
        return "1";
    }
    @ResponseBody
    @RequestMapping("/order")
    public String order(String count,String price,String product,HttpSession session){
        TbUser user = (TbUser)session.getAttribute("user");
        TbOrder tbOrder = new TbOrder(idUtils.suijiutils(), count, price, product, user.getUserTel());
        tbHotelService.orderInsert(tbOrder);
        return "1";
    }
}

